import 'package:syria_sports/presentation/exercises/public_exercises_page/widgets/exercise_item_view.dart';
import '../../../../widgets/app_bar/appbar_subtitle.dart';
import '../../../../widgets/app_bar/custom_app_bar.dart';
import '../../../../widgets/app_bar/outline_icon_button.dart';
import '../../../../widgets/screen_state_widget.dart';
import '../../../gyms/public_gyms_page/models/public_gym.dart';
import 'exercises_cubit.dart';
import 'package:flutter/material.dart';
import 'package:syria_sports/core/app_export.dart';

class PublicExercisesScreen extends StatelessWidget {
  final PublicGym? gym;
  const PublicExercisesScreen({Key? key, this.gym}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    double height = MediaQuery.of(context).size.height;
    double width = MediaQuery.of(context).size.width;
    var exercisesCubit = ExercisesCubit();

    return Scaffold(
      appBar:_buildAppBar(context:context ,cubit: exercisesCubit),
      body: BlocProvider<ExercisesCubit>(
        create: (context) => exercisesCubit..init(gym:gym),
        child: BlocConsumer<ExercisesCubit, ExercisesState>(
          listener: (context, state) {
            // TODO: implement listener
          },
          builder: (context, state) {
            var cubit = ExercisesCubit.get(context);

            return Container(
              height: height,
              width: width,
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage(ImageConstant.imgBackground),
                  fit: BoxFit.cover,
                ),
              ),
              child: ScreenStateWidget(
                isDone: state is ExercisesDone,
                isError: state is ExercisesError,
                isLoading: state is ExercisesLoading,
                onRefresh: cubit.getAllGyms,
                onDone: ListView.builder(
                  itemCount:cubit.exercises.length,
                  itemBuilder: (context,index){
                    return ExerciseItemView(exercise:cubit.exercises[index]);
                  },
                ),
              ),

            );
          },
        ),
      ),
    );
  }
  PreferredSizeWidget _buildAppBar({required BuildContext context, required ExercisesCubit cubit}) {
    return CustomAppBar(
        leading: OutlineIconButton(
          imagePath: ImageConstant.imgRightIcon,
          onTapped: (){
            Navigator.pop(context);
          },
        ),
        title: AppbarSubtitle(
            text: "التمارين", margin: EdgeInsets.only(left: 12.h),
        ),
    );
  }

}
